package com.hk.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hk.annotation.AutoCheck;
import com.hk.common.Constant;
import com.hk.common.ResponseResult;
import com.hk.enums.SpaceLevelEnum;
import com.hk.model.vo.IdVO;
import com.hk.model.vo.space.*;
import com.hk.service.SpaceService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author huangkun
 * @date 2024/12/11 21:51
 */
@RestController
//@Api(value = "空间管理", tags = "空间管理")
@RequestMapping("/space")
public class SpaceController {

    @Resource
    private SpaceService spaceService;


    @ApiOperation(value = "添加空间")
    @PostMapping("/add")
    public ResponseResult<SpaceVO> addSpace(@RequestBody SpaceOperateVO spaceOperateVO) {
        return ResponseResult.success(spaceService.addSpace(spaceOperateVO));
    }

    @ApiOperation(value = "修改空间")
    @PostMapping("/update")
    public ResponseResult<SpaceVO> updateSpace(@RequestBody SpaceOperateVO spaceOperateVO) {
        return ResponseResult.success(spaceService.updateSpace(spaceOperateVO));
    }


    @ApiOperation(value = "根据id获取空间信息")
    @GetMapping("/get/id")
    public ResponseResult<SpaceVO> getSpaceInfo(@RequestParam Long id) {
        return ResponseResult.success(spaceService.getSpaceInfo(id));
    }

    @ApiOperation(value = "根据用户id获取私人空间信息")
    @GetMapping("/get/user/id")
    public ResponseResult<SpaceVO> getSpaceInfoByUser(@RequestParam Long userId) {
        return ResponseResult.success(spaceService.selectSpaceByUser(userId));
    }

    @ApiOperation(value = "获取空间分页列表")
    @PostMapping("/page")
    @AutoCheck(mustRole = Constant.ROLE_ADMIN)
    public ResponseResult<Page<SpaceVO>> getSpacePageList(@RequestBody SpaceQueryParamVO queryParamVO) {
        if (queryParamVO.getCurrent() <= 0) {
            queryParamVO.setCurrent(1);
        }

        if (queryParamVO.getPageSize() <= 0) {
            queryParamVO.setPageSize(10);
        }
        if (queryParamVO.getPageSize() > 999) {
            queryParamVO.setPageSize(999);
        }
        return ResponseResult.success(spaceService.getSpacePageList(queryParamVO));
    }

    @ApiOperation(value = "编辑空间,需要管理员权限")
    @PostMapping("/edit")
    @AutoCheck(mustRole = Constant.ROLE_ADMIN)
    public ResponseResult<SpaceVO> editSpace(@RequestBody SpaceEditVO spaceEditVO) {
        return ResponseResult.success(spaceService.editSpace(spaceEditVO));
    }


    @ApiOperation(value = "删除空间")
    @PostMapping("/delete")
    public ResponseResult deleteSpace(@RequestBody IdVO idVO) {
        spaceService.delete(idVO);
        return ResponseResult.success();
    }

    @ApiOperation(value = "获取空间级别列表")
    @GetMapping("/level/list")
    public ResponseResult<List<SpaceLevelVO>> getSpaceLevelList() {
        List<SpaceLevelVO> levelVOList = Arrays.stream(SpaceLevelEnum.values()).map(levelEnum ->
                new SpaceLevelVO(levelEnum.getText(), levelEnum.getValue(), levelEnum.getMaxSize(), levelEnum.getMaxCount()))
                .collect(Collectors.toList());
        return ResponseResult.success(levelVOList);
    }


}

